<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * --------------------------------------------------------------------------
 * KULLANICILAR ADMIN TARAFI KONTROL DOSYASI
 * --------------------------------------------------------------------------
 * Kullanıcı CRUD işlemleri bu kontrolör 'da yapılır.
 *
 * @package user
 * @author  Ali OYGUR
 * @since   2011-04-02
 **/
class User extends Admin_Controller {

	public function __construct()
	{
        parent::__construct();
    }

	public function add()
	{
    }

	public function index()
	{
		$limit  = 20;
        $offset = _get('per_page', 0);
   		
	   	$role_id = _get('role_id', 'all');
        $needle  = _get('needle', NULL);

        // get users
        if($needle)
        {
            $result = $this->user_model->search($needle, $limit, $offset);
        }
        else
        {
            $result = $this->user_model->gets(array('role_id' => $role_id), $limit, $offset);
        }

        // pagination
        $pagination = create_pagination(uri_string().'/?needle='.$needle, $result['total_rows'], $limit);

		// user roles
        $sql = "SELECT id, name FROM {$this->db->dbprefix('user_role')} ORDER BY id";
		$roles = dropdown_fromdb($sql);
		$roles = array('all' => 'Seviye Seçin') + $roles;

        $data = array(
			'result' => $result,
			'pagination' => $pagination,
			'roles' => $roles
        );

        $this->template->load('user/index', $data);
	}

	public function detail($id)
	{
        // user
        $user = $this->user_model->get(array('id' => $id)) OR show_404();

        // DATA
        $data = array(
            'user' => $user
        );

        $this->template->load('user/detail', $data);
    }

	public function edit_account($id)
	{
		if (empty($_POST))
		{
            // user
            $user = $this->user_model->simple_get($id);
            
            // user roles
            $sql = "SELECT id, name FROM {$this->db->dbprefix('user_role')}";
            $roles = dropdown_fromdb($sql);

            // DATA
            $data = array(
                'user' => $user,
                'roles' => $roles
            );

            $this->load->view('backend/user/edit_account', $data);
		}
		else
		{
            // VALIDATION
            $this->load->library('form_validation');
            $this->form_validation->set_rules('email', 'E posta', 'trim|required|valid_email|check_email2');

            // if changed password
			if (isset($_POST['change_password']))
			{
                $this->form_validation->set_rules('password', 'Şifre', 'trim|required|matches[passwordconf]');
                $this->form_validation->set_rules('passwordconf', 'Şifre Doğrulama', 'trim|required');
            }

			if (!$this->form_validation->run())
			{
            	ajax_response($this->form_validation->error_string());
            }

            // CONTINUE

            $data = array(
                'email' => $_POST['email'],
                'role_id' => $_POST['role_id']
            );

			if (isset($_POST['change_password']))
			{
                $salt = $this->auth->generate_salt();
                $passwordHash = do_hash($salt . $_POST['password']);

                $data['salt'] = $salt;
                $data['password_hash'] = $passwordHash;
            }

            $this->user_model->edit($data, array('id' => $id));

			ajax_response('İşlem başarıyla tamamlandı', FALSE);
        }
    }

	public function edit_profile($id)
	{
        $this->load->model('user_profile_model');

		if (empty($_POST))
		{
            // user
            $user = $this->user_profile_model->get($id);

            // DATA
            $data = array(
                'user' => $user
            );

            $this->load->view('backend/user/edit_profile', $data);
		}
		else
		{
            // VALIDATION
            $this->load->library('form_validation');
            $this->form_validation->set_rules('firstname', 'Ad', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('lastname', 'Soyad', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('birthday', 'Doğum Tarihi', 'trim|required|xss_clean|exact_length[10]');
            $this->form_validation->set_rules('telephone', 'Telefon', 'trim|required|xss_clean|exact_length[14]');
            $this->form_validation->set_rules('address', 'Adres', 'trim|required|xss_clean');
            $this->form_validation->set_rules('city', 'İl', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('town', 'İlçe', 'trim|required|xss_clean|max_length[255]');

			if (!$this->form_validation->run())
			{
               	ajax_response($this->form_validation->error_string());
            }

            // CONTINUE

            $this->user_profile_model->edit($_POST, array('user_id' => $id));

			ajax_response('İşlem başarıyla tamamlandı', FALSE);
        }
    }

	public function delete($id)
	{
        $this->user_model->delete($id);
		
		$msg = 'Kullanıcı Başarıyla Silindi.';
        set_notify($msg);
        
        redirect($this->input->server('HTTP_REFERER'));
    }
	
	public function logout()
	{
        $this->auth->logout();
        redirect('panel');
	}
}

/* End of file user.php */
